<?php

namespace App\Http\Controllers\Admin\Collection;

use App\Http\Controllers\Controller;
use App\Models\Admin\Collection\CollectionLogin;
use App\Models\Common\CollArea;
use Illuminate\Http\Request;
use Style;

/**
 * 公共采集类
 * Class FiveEight
 * @package App\Http\Controllers\Admin\Collection
 */
class Common extends Controller{

    /**
     * 快速登录表
     * @param $coll_url //采集的地址
     * @param $username //采集的用户名
     * @param $password //采集的密码
     * @param $source //采集来源
     * @param $type //采集的类型  1代表简历账户 2代表企业账户
     */
    static public function fastLogin($coll_url, $username, $password, $source, $type){
        $collectionLogin = CollectionLogin::where('username', $username)->where('source', $source)->where('type', $type)->first();
        if(!empty($collectionLogin)){
            CollectionLogin::where('username', $username)->where('source', $source)->where('type', $type)->update(['url' => $coll_url]);
        }else{
            CollectionLogin::create([
                'url' => $coll_url,
                'username' => $username,
                'password' => $password,
                'source' => $source,
                'type' => $type
            ]);
        }
    }

    /**
     * 检查用户是不是和上一次用户相同
     * @param $username
     * @param $source
     * @param $type 1代表简历账户 2代表企业账户
     * @return bool
     */
    static public function checkUsername($username, $source, $type){
        $collectionLogin = CollectionLogin::where('source', $source)->where('type', $type)->orderBy('updated_at', 'desc')->first(['username']);
        if(!empty($collectionLogin)){
            if($collectionLogin->username == $username){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 多个地区匹配相似度
     * @param $collArea
     * @param $location
     */
    static public function similarity($collArea, $location){
        $arr = [];
        if(count($location) > 0){
            $jobAreaArr = [];
            for($i = 0; $i < count($location); $i++){
                foreach($collArea as $key => $val){
                    similar_text($location[$i], $val->name, $percent);    //匹配相似度
                    $jobAreaArr[$i][$val->cid] = $percent;
                }
                arsort($jobAreaArr[$i]);
                $jobLocation = array_slice(array_keys($jobAreaArr[$i]), 0, 1);
                $arr[] = $jobLocation[0];
            }
        }
        return $arr;
    }

    /**
     * 单个匹配地区相似度
     * @param $collArea
     * @param $location
     */
//    static public function signSimilarity($collArea, $area){
//        if(!empty($area)){
//            foreach($collArea as $key => $val){
//                similar_text($area, $val->name, $percent);    //匹配相似度
//                $jobArea[$val->cid] = $percent;
//            }
//            arsort($jobArea);
//            $jobLocation = array_slice(array_keys($jobArea), 0, 1);
//            $area = $jobLocation[0];
//
//        }
//        return $area;
//    }

    /**
     * 单个匹配地区相似度
     * @param $collArea
     * @param $location
     */
    static public function signSimilarity($collArea, $area){
        $jobArea = [];
        $arr = [];
        if(!empty($area)){
            foreach($collArea as $key => $val){
                $two_location = preg_replace('/(区|县|市)/', '', $val->name);
                if(strstr($area, $two_location)){
                    $jobArea[] = $val->cid;
                }
            }
        }
        if(count($jobArea) > 0){
            arsort($jobArea);
            foreach($jobArea as $key => $val){
                similar_text($area, CollArea::getAreaName($val), $percent);    //匹配相似度
                $arr[$val] = $percent;
            }
            $arr = array_unique($arr);
            arsort($arr);
            $jobLocation = array_slice(array_keys($arr), 0, 1);
            $area = $jobLocation[0];
            return $area;
        }
        return '';
    }

    /**
     * 匹配岗位数组相似度
     * @param $collArea
     * @param $area
     */
    static public function matchJob($job_name, $jobArray){
        if(count($jobArray) > 0){
            //查找键名是不是存在
            if(array_key_exists($job_name, $jobArray)){
                return $jobArray[$job_name];
            }else{
                return '';
            }
        }else{
            return '';
        }
    }

    /**
     * 匹配语言种类 技能 等...
     ** @param $name
     * @param $array
     * @return string
     */
    static public function matchOption($name, $array){
        if(count($array) > 0){
            if(in_array($name, array_column($array, 'text'))){
                foreach($array as $key => $val){
                    if($name == $val['text']){
                        return $val['value'];
                    }
                }
            }else{
                return '';
            }
        }else{
            return '';
        }
    }
}